//
//  LotteryTicketViewController.m
//  NomalTest
//
//  Created by 陈小东 on 15/6/18.
//  Copyright (c) 2015年 &#26477;&#24030;&#26102;&#25237;&#20449;&#24687;&#31185;&#25216;@&#38472;&#23567;&#19996;. All rights reserved.
//

#import "LotteryTicketViewController.h"
#import <MJRefresh.h>
#import <NSDate+Nomal.h>

@interface LotteryTicketViewController ()
<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,strong) NSArray *nameArray;
@property (nonatomic,strong) NSArray *nameCodeArray;
@property (nonatomic,strong) NSMutableArray *dataArray;

@end

static NSString *kLotteryTicketCellIdentifier = @"kLotteryTicketCellIdentifier";

@implementation LotteryTicketViewController

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kUseViewScreenHeight)
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"LotteryTicket";
    self.nameArray = @[@"山东十一选五",@"江西十一选五",@"重庆时时彩",@"江西时时彩"];
    self.nameCodeArray = @[@"sd11x5",@"jx11x5",@"cqssc",@"jxssc"];
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    [self.tableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadLotteryTicketData)];
    [self loadLotteryTicketData];
    
}

- (void)loadLotteryTicketData{
    [HttpRequsetApi fetchLotteryList];
    
    WS(weakSelf);
    for(NSString *code in self.nameCodeArray){
        [HttpRequsetApi fetchLotteryQuery:code
                                  success:^(id data) {
                                      [weakSelf.tableView.header endRefreshing];
                                      [weakSelf.dataArray addObject:data];
                                      [weakSelf.tableView reloadData];
                                  } failure:^(NSError *error) {
                                      [weakSelf.tableView.header endRefreshing];
                                  }];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kLotteryTicketCellIdentifier];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:kLotteryTicketCellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = kFormat(@"%@", self.nameArray[indexPath.row]);
    
    NSString *lotteryCode = self.nameCodeArray[indexPath.row];
    
    for(LotteryTicketItem *item in self.dataArray){
        if([lotteryCode isEqualToString:item.lotteryCode]){
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"HH:mm:ss"];
            NSString *openTime = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:item.openTimeStamp]];
            cell.detailTextLabel.text = kFormat(@"%@ --> %@ --> %@",item.expect,openTime,item.openCode);
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
